/*
 * @Author: 徐建辰
 * @Date: 2022-02-12 15:20:34
 * @LastEditTime: 2022-02-15 14:58:51
 * @LastEditors: 徐建辰
 * @Description: 路由鉴权，仅针对C端用户
 */
import nprogress from 'nprogress'
import router from './index'
import {getToken} from '@/utils/local'
import {getDocumentTitle} from '@/utils/utils'

const whitelist = ['/login']

router.beforeEach((to, from, next) => {
  if (to.name === undefined) {
    next('/404')
    return
  }
  nprogress.start()
  if (getToken()) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whitelist.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

router.afterEach(to => {
  nprogress.done()
  document.title = getDocumentTitle(to)
})
